#include "BasicProfile.h"

namespace utility
{
	BasicProfile::BasicProfile(void)
	{
	}

	BasicProfile::BasicProfile( cstr_t lpszFileName ) : m_strFileName(lpszFileName)
	{

	}
	BasicProfile::~BasicProfile(void)
	{
	}

	void BasicProfile::Init( cstr_t lpszFileName )
	{
		m_strFileName = lpszFileName;
	}

	bool BasicProfile::WriteInteger( cstr_t lpszAppName, cstr_t lpszKeyName, int nValue )
	{
		char_t buf[32] = {0};
		_stprintf_s( buf ,32, _T("%d"), nValue );

		return WritePrivateProfileString( lpszAppName, lpszKeyName, buf, m_strFileName.c_str() ) == TRUE;
	}

	bool BasicProfile::WriteString( cstr_t lpszAppName, cstr_t lpszKeyName, cstr_t lpszValue )
	{
		return WritePrivateProfileString( lpszAppName, lpszKeyName, lpszValue, m_strFileName.c_str() ) == TRUE;
	}

	bool BasicProfile::WriteStruct( cstr_t lpszAppName, cstr_t lpszKeyName, void * pStruct, uint32 nSizeStruct )
	{
		return WritePrivateProfileStruct( lpszAppName, lpszKeyName, pStruct, nSizeStruct, m_strFileName.c_str() ) == TRUE;
	}

	int32 BasicProfile::ReadInteger( cstr_t lpszAppName, cstr_t lpszKeyName, int32 nDefault )
	{
		return GetPrivateProfileInt( lpszAppName, lpszKeyName, nDefault, m_strFileName.c_str() );
	}

	int64 BasicProfile::ReadInteger64( cstr_t lpszAppName, cstr_t lpszKeyName, int64 nDefault )
	{
		char_t buf[48];
		int nSize = GetPrivateProfileString( lpszAppName, lpszKeyName, _T("0"), buf, 48, m_strFileName.c_str() );
		return _tstoi64( buf );
	}

	uint32 BasicProfile::ReadString( cstr_t lpszAppName, cstr_t lpszKeyName, cstr_t lpDefault, string_t & strValue )
	{
		char_t buf[256] = {0};
		uint32 nSize = GetPrivateProfileString( lpszAppName, lpszKeyName, lpDefault, buf, 256, m_strFileName.c_str() );
		if( nSize > 0 )
		{
			strValue = buf;
		}
		return nSize;
	}

	bool BasicProfile::ReadStruct( cstr_t lpszAppName, cstr_t lpszKeyName ,void * pStruct, uint32 nSizeStruct )
	{
		return GetPrivateProfileStruct( lpszAppName, lpszKeyName, pStruct, nSizeStruct, m_strFileName.c_str() )==TRUE;
	}

	int BasicProfile::GetAllSectionNames( char_t * pBuf, int nBufLen )
	{
		return GetPrivateProfileSectionNames( pBuf, nBufLen, m_strFileName.c_str() );
	}
}

